﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for AirfareBusiness
/// </summary>
public class AirfareBusiness
{
	public AirfareBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public bool Insert(AirfareEntities item)
    {
        bool flat = false;
        string[] para = { "@flightID", "@userID", "@classID", "@airfareTypeID", "@cost" };
        string[] values = { item._flightID.ToString(), item._userID.ToString(), item._classID.ToString(),item._airfareTypeID.ToString() ,item._cost.ToString()};
        if (DataAccess.getExecProcedure("sp_InsertAirfare", para, values) > 0)
        {
            flat = true;
        }
        else
            flat = false;
        return flat;
    }

    public bool Insert(string[] values)
    {
        bool flat = false;
        string[] para = { "@flightID", "@userID", "@classID", "@airfareTypeID", "@cost" };
        
        if (DataAccess.getExecProcedure("sp_InsertAirfare", para, values) > 0)
        {
            flat = true;
        }
        else
            flat = false;
        return flat;
    }

    public bool Edit(AirfareEntities item)
    {
        bool flat = false;
        string[] para = { "@airfareID", "@flightID", "@userID", "@classID", "@airfareTypeID", "@cost" };
        string[] values = { item._airfareID.ToString(), item._flightID.ToString(), item._userID.ToString(), item._classID.ToString(), item._airfareTypeID.ToString(),item._cost.ToString() };
        if (DataAccess.getExecProcedure("sp_UpdateAirfare", para, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Edit(string[] values)
    {
        bool flat = false;
        string[] para = { "@airfareID", "@flightID", "@userID", "@classID", "@airfareTypeID", "@cost" };
        
        if (DataAccess.getExecProcedure("sp_UpdateAirfare", para, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Delete(int airfareID)
    {
        if (DataAccess.getExec("exec sp_DeleteAirfare '"+airfareID+"'") > 0)
            return true;
        else
            return false;
    }

    public DataTable getAllAirfare()
    {
        return DataAccess.DataTable_Query("exec sp_GetAirfare ''");
    }

    public DataTable getAirfareByUser(int userID)
    {
        return DataAccess.DataTable_Query("exec sp_GetAirfareByUser '" + userID + "'");
    }

    public AirfareEntities getAirfare(int id)
    {
        AirfareEntities ae = new AirfareEntities();
        DataTable dt = DataAccess.DataTable_Query("exec sp_GetAirfare '"+id+"'");
        if (dt != null)
        {
            DataRow dr = dt.Rows[0];
            ae._airfareID = int.Parse(dr["airfareID"].ToString());
            ae._flightID = int.Parse(dr["flightID"].ToString());
            ae._userID = int.Parse(dr["userID"].ToString());
            ae._classID = int.Parse(dr["classID"].ToString());
            ae._airfareTypeID = int.Parse(dr["airfareTypeID"].ToString());
            ae._cost = Convert.ToDouble(dr["cost"].ToString());
        }
        return ae;
    }

    public double getPrice(double flightPrice, int totalNguoiLon, int totalTreEm, double classFactor, double promotionsFactor,double tax,string khuhoi)
    {
        AirfareTypeBusiness atb = new AirfareTypeBusiness();
        double price = 0;
        if (totalNguoiLon > 0)
        {
            for (int i = 1; i <= totalNguoiLon; i++)
            {
                price += (flightPrice + flightPrice * classFactor);
            }
        }
        
        if (totalTreEm > 0)
        {
            for (int j = 1; j <= totalTreEm; j++)
            {

                double priceNew = (flightPrice - flightPrice * atb.getAirfareType(2)._airfareTypeFactor);
                price += (priceNew * classFactor + priceNew);
            }
        }
        if(promotionsFactor!=1)
            price -= price * promotionsFactor;
        if(khuhoi=="True")
            price-=price*20/100;
        price += price * tax;
        return price;
    }

    public DataTable getAirfareBy(string[]TableName,string[]fields,string Criterias,string priorities)
    {
        DataTable dt = new DataTable();
        string query = SQLQuery.SQLQuery_SELECT(TableName, fields, Criterias, priorities);
        return dt=DataAccess.DataTable_Query(query);
    }
    public int coutAirfare(int flightID, int classID)
    {
        int count = (int)DataAccess.getScalar("exec sp_CountAirfare '"+flightID+"', '"+classID+"'");
        return count;
    }
}